package com.lelefly.base.serviceImp;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Id;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.model.Result;
import com.lelefly.base.po.FarmlandSpray;
import com.lelefly.base.po.FarmlandTask;
import com.lelefly.base.po.UavInfo;
import com.lelefly.constant.Constant;
import com.lelefly.handleexecption.ServiceExecption;
import com.lelefly.handleexecption.ValidateExecption;

@Service
public class FarmlandSprayService {

	
	@Resource
	public BaseDao baseDao;
	
	
	
	
	@SuppressWarnings({ "null", "rawtypes" })
	public Result getFarmlandSpray(String usernm) {
		Result<FarmlandSpray> result = new Result<>();
		try {
			if (StringUtils.isEmpty(usernm) ) {
				throw new ServiceExecption("参数错误");
			}
			String sql = "select model  from FarmlandInfo a ,FarmlandSpray as model "
					+ "where a.farmlandnm = b.farmlandnm and a.usernm='"+usernm+"' ";
			List<FarmlandSpray> farmlandTasks= baseDao.queryObjetReList(sql);
			result.setState(Constant.HTTP_STATUS_OK);
			result.setResultList(farmlandTasks);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	@SuppressWarnings({ "null", "rawtypes" })
	public Result addFarmlandSpray(FarmlandSpray farmlandSpray) {
		Result<FarmlandTask> result = new Result<>();
		try {
			baseDao.saveObject(farmlandSpray);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}
		return result;
	}
	
	
	@SuppressWarnings("null")
	public Result delFarmlandSpray(String id ) {
		Result<FarmlandTask> result = new Result<>();
		try {
			if (StringUtils.isEmpty(id)) {
				throw new ServiceExecption("删除条件为空");
			}
			String sql = "delete FarmlandSpray as model where id='"+id+"' "; 
			baseDao.deteleObjectbySql(sql);
			result.setState(Constant.HTTP_STATUS_OK);
		}catch(Exception e){
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(Constant.HTTP_STATUS_DEFERROR);
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
}
